#!/bin/bash

############################
# 本程序用于一键安装mysql8 前提是将mysql安装包放入/opt目录下。
# 本程序调用方法：./mysql一键安装.sh
# 作者： 2021年6月3日
############################

echo "设置mysql_base"
MYSQL_BASE=/home/mysql-8.0.25

echo "安装依赖"
yum -y install libaio
yum -y install ncurses-compat-libs
yum -y install gcc 

echo "创建用户组"
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
cd /opt/
tar xvf /opt/mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz -C /home
mv /home/mysql-8.0.25-linux-glibc2.12-x86_64 $MYSQL_BASE
cd $MYSQL_BASE
mkdir etc var
cd etc
echo "写入配置文件"
cat >> my.cnf <<EOF
[client]
port = 3306
socket = $MYSQL_BASE/mysql.sock
[mysqld]
port = 3306
socket = $MYSQL_BASE/mysql.sock
basedir = $MYSQL_BASE
datadir = $MYSQL_BASE/data
pid-file = $MYSQL_BASE/var/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1 #表示是本机的序号为1,一般来讲就是master的意思
skip-name-resolve
back_log = 600
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 8
#query_cache_size = 8M
#query_cache_limit = 2M
key_buffer_size = 4M
ft_min_word_len = 4
transaction_isolation = REPEATABLE-READ
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 30 #超过30天的binlog删除
log_error = $MYSQL_BASE/var/mysql-error.log #错误日志路径
slow_query_log = 1
long_query_time = 1 #慢查询时间 超过1秒则为慢查询
slow_query_log_file = $MYSQL_BASE/var/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
skip-external-locking #MySQL选项以避免外部锁定。该选项默认开启
default-storage-engine = InnoDB #默认存储引擎
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M #服务器发送和接受的最大包长度
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
EOF

echo "修改用户属组"
chown -R mysql:mysql $MYSQL_BASE 
chmod 750 etc var
echo "初始化myql  defaults-file必须是第一个参数"
$MYSQL_BASE/bin/mysqld  --defaults-file=$MYSQL_BASE/etc/my.cnf --initialize --user=mysql --basedir=$MYSQL_BASE --datadir=$MYSQL_BASE/data  

sleep 10

echo "获取mysql初始密码"
PASSWORD=`cat $MYSQL_BASE/var/mysql-error.log |grep "temporary password"|awk -F"root@localhost: " '{print $2}'`

$MYSQL_BASE/bin/mysql_ssl_rsa_setup  --datadir=$MYSQL_BASE/data/
$MYSQL_BASE/bin/mysqld_safe  --defaults-file=$MYSQL_BASE/etc/my.cnf  --user=mysql --basedir=$MYSQL_BASE --datadir=$MYSQL_BASE/data &
# Next command is optional

echo "修改mysql密码"
$MYSQL_BASE/bin/mysql --socket=$MYSQL_BASE/mysql.sock --connect-expired-password -uroot -p${PASSWORD} -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'mypassword';"

